# -*- coding: UTF-8 -*-
from flask import Flask,redirect, url_for,flash,jsonify
from flask import render_template
from flask import request
import requests
import time
import json
import random

app = Flask(__name__)

#http://127.0.0.1:5000/
@app.route('/')
def Home():
    return render_template('Home.html')

##http://127.0.0.1:5000/homepage
@app.route('/Homepage')
def Homepage():
    return render_template('Homepage.html')

##http://127.0.0.1:5000/firstFage
@app.route('/FirstFage')
def FirstFage():
    # print(requests.get("http://127.0.0.1:5000/connectstatus"))
    # tmp=list(requests.gst("http://127.0.0.1:5000/connectstatus",json={key:value}))

    dict_tmp = {"com1": "grey", "com2": "grey", "com3": "grey", "com4": "grey", "com5": "red"
        , "com6": "grey", "com7": "red", "com8": "green", "com9": "red", "com10": "red"
        , "com11": "red", "com12": "green", "com13": "red", "com14": "green", "com15": "red"
        , "com16": "green", "com17": "green", "com18": "green", "com19": "red", "com20": "grey"
        , "com21": "grey", "com22": "grey", "com23": "red", "com24": "grey", "com25": "red"
        , "com26": "green", "com27": "red", "com28": "green", "com29": "red", "com30": "grey"
        , "com31": "green", "com32": "grey"}

    return render_template("FirstFage.html",
                           com1="background-color:{};".format(dict_tmp["com1"]),
                           com2="background-color:{};".format(dict_tmp["com2"]),
                           com3="background-color:{};".format(dict_tmp["com3"]),
                           com4="background-color:{};".format(dict_tmp["com4"]),
                           com5="background-color:{};".format(dict_tmp["com5"]),
                           com6="background-color:{};".format(dict_tmp["com6"]),
                           com7="background-color:{};".format(dict_tmp["com7"]),
                           com8="background-color:{};".format(dict_tmp["com8"]),
                           com9="background-color:{};".format(dict_tmp["com9"]),
                           com10="background-color:{};".format(dict_tmp["com10"]),
                           com11="background-color:{};".format(dict_tmp["com11"]),
                           com12="background-color:{};".format(dict_tmp["com12"]),
                           com13="background-color:{};".format(dict_tmp["com13"]),
                           com14="background-color:{};".format(dict_tmp["com14"]),
                           com15="background-color:{};".format(dict_tmp["com15"]),
                           com16="background-color:{};".format(dict_tmp["com16"]),
                           com17="background-color:{};".format(dict_tmp["com17"]),
                           com18="background-color:{};".format(dict_tmp["com18"]),
                           com19="background-color:{};".format(dict_tmp["com19"]),
                           com20="background-color:{};".format(dict_tmp["com20"]),
                           com21="background-color:{};".format(dict_tmp["com21"]),
                           com22="background-color:{};".format(dict_tmp["com22"]),
                           com23="background-color:{};".format(dict_tmp["com23"]),
                           com24="background-color:{};".format(dict_tmp["com24"]),
                           com25="background-color:{};".format(dict_tmp["com25"]),
                           com26="background-color:{};".format(dict_tmp["com26"]),
                           com27="background-color:{};".format(dict_tmp["com27"]),
                           com28="background-color:{};".format(dict_tmp["com28"]),
                           com29="background-color:{};".format(dict_tmp["com29"]),
                           com30="background-color:{};".format(dict_tmp["com30"]),
                           com31="background-color:{};".format(dict_tmp["com31"]),
                           com32="background-color:{};".format(dict_tmp["com32"])
                           )

##http://127.0.0.1:5000/Showobject
@app.route('/Showobject')
def Showobject():
    return render_template('Showobject.html')

##http://127.0.0.1:5000/deep
@app.route('/deep')
def deep():
    return render_template('deep.html')

@app.route('/01')
def a01():
    return render_template('01.html')

@app.route('/02')
def a02():
    return render_template('02.html')
@app.route('/03')
def a03():
    return render_template('03.html')
##http://127.0.0.1:5000/protect
@app.route('/protect')
def protect():
    return render_template('protect.html')

##http://127.0.0.1:5000/speed
@app.route('/speed')
def speed():

    #按接口的数据
    dict_tmp = {"name": "转油一线", "All_Time": "2015-12-25 14:08:45", "All_CR_max": "0.478042",
                "All_CR_avg": "0.169100", "All_LC_Rate_Factor": "2.826967",
                "data": []}

    for i in range(5):
        tmp = {}
        #    # tmp["Number"] = str(i)
        tmp["Time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(1665058704 + 2 * i))
        tmp["CR_max"] = str(float(random.randint(0, 1000000)) / 1000000)
        tmp["CR_avg"] = str(float(random.randint(0, 1000000)) / 1000000)
        tmp["LC_Rate_Factor"] = str(float(random.randint(0, 200)) / 100)
        dict_tmp["data"].append(tmp)
        del tmp
    print(dict_tmp)
    timetmp = []
    cr_max = []
    cr_avg = []
    lc = []
    for a in dict_tmp["data"]:
        # print(a)
        for key, value in a.items():
            if key == "Time":
                timetmp.append(str(value))
            if key == "CR_max":
                cr_max.append(float(value))
            if key == "CR_avg":
                cr_avg.append(float(value))
            if key == "LC_Rate_Factor":
                lc.append(float(value))
    # print(time)
    # print(cr_max)
    # print(cr_avg)
    # print(lc)
    return render_template("speed.html",
                           time=timetmp, cr_max=cr_max, cr_avg=cr_avg, lc=lc,
                           a=5000, b=timetmp[1], c=cr_max[1], d=cr_avg[1], e=lc[1])
#################data get###########################################################################
#http://127.0.0.1:5000/checkstatusdata
@app.route('/checkstatusdata')
def checkstatusdata():
    dict_tmp={"status1": "green","status2": "red",
              "status3": "grey", "status4": "red",
              "status5": "red", "status6": "red"}
    return jsonify(dict_tmp)

#http://127.0.0.1:5000/corrosion/connectstatusdata
@app.route('/corrosion/connectstatusdata')
def corrosionconnectstatusdata():
    dict_tmp={"com1":"red","com2":"green","com3":"green","com4":"grey","com5":"red"
              ,"com6":"grey","com7":"red","com8":"green","com9":"red","com10":"red"
              ,"com11":"red","com12":"green","com13":"red","com14":"green","com15":"red"
              ,"com16":"green","com17":"green","com18":"green","com19":"red","com20":"grey"
              ,"com21":"grey","com22":"grey","com23":"red","com24":"grey","com25":"red"
              ,"com26":"green","com27":"red","com28":"green","com29":"red","com30":"grey"
              ,"com31":"green","com32":"grey"}
    return jsonify(dict_tmp)

#中文是utf-8
#http://127.0.0.1:5000/corrosion/speedmaxdata
@app.route('/corrosion/speedmaxdata')
def corrosionspeedmaxdata():
    dict_tmp={"name": "转油一线","All_Time":"2015-12-25 14:08:45","All_CR_max":"0.478042",
                "All_CR_avg": "0.169100","All_LC_Rate_Factor": "2.826967",
              "data": []}

    for i in range(20):
        tmp = {}
        # tmp["Number"] = str(i)
        tmp["Time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(1665058704 + 2 * i))
        tmp["CR_max"] = str(float(random.randint(0, 1000000)) / 1000000)
        tmp["CR_avg"] = str(float(random.randint(0, 1000000)) / 1000000)
        tmp["LC_Rate_Factor"] = str(float(random.randint(0, 200)) / 100)
        dict_tmp["data"].append(tmp)
        del tmp
    return jsonify(dict_tmp)

#http://127.0.0.1:5000/corrosion/speedchoicedata
@app.route('/corrosion/speedchoicedata')
def corrosionspeedchoicedata():
    # 按文档发送数据
    dict_tmp={}
    for i in range(32):
        data_tmp = {}
        data_tmp["ID1"]="转油"+str(i)+"线上弯头"
        data_tmp["ID2"] = "转油" + str(i) + "线上弯头2"
        data_tmp["ID3"] = str(i) + " 常减压基础桩"

        tmp_str="COM"+str(i)
        dict_tmp[tmp_str]=data_tmp
        del data_tmp
    return jsonify(dict_tmp)

#http://127.0.0.1:5000/corrosion/multiplicityshowMVdata/<int:COM>
@app.route('/corrosion/multiplicityshowMVdata/<int:COM>')
def corrosionmultiplicityshowMVdata(COM):
    dict_tmp = {"name": "转油一线","data": [{ "Time": "2015-12-25 14:08:45", "CR_max": "0.478042"}]}
    return jsonify(dict_tmp)

#http://127.0.0.1:5000/corrosion/multiplicityshowAVdata/<int:COM>
@app.route('/corrosion/multiplicityshowAVdata/<int:COM>')
def corrosionmultiplicityshowAVdata(COM):
    dict_tmp = {"name": "转油一线",
	"data": [{ "Time": "2015-12-25 14:08:45", "CR_avg": "0.169100"}]}
    return jsonify(dict_tmp)

#http://127.0.0.1:5000/corrosion/multiplicityshowVFdata/<int:COM>
@app.route('/corrosion/multiplicityshowVFdata/<int:COM>')
def corrosionmultiplicityshowVFdata(COM):
    dict_tmp = {"name": "转油一线",
	"data": [{ "Time": "2015-12-25 14:08:45", "CR_avg": "0.169100"}]}
    return jsonify(dict_tmp)

#http://127.0.0.1:5000/corrosion/multiplicityshowdata
@app.route('/corrosion/multiplicityshowdata', methods=['GET', 'POST'])
def corrosionmultiplicityshowdata():
    if request.method == "POST":
        str_tmp=request.stream.read() #get all data
        try:
            dict_tmp=json.loads(str_tmp)# note unicode
        except:
            return jsonify({"result": "data error"})
        print(str_tmp)
        #print(type(request.stream.read()))#post的内容
        #
        result={"result": "ok"}
        return jsonify(result)
    elif request.method == "GET":
        result = {}
        list_tmp = []
        dict_tmp = {"name": "转油一线", "data": []}

        for i in range(20):
            tmp = {}
            # tmp["Number"] = str(i)
            tmp["Time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(1665058704 + 2 * i))
            tmp["CR_max"] = str(float(random.randint(0, 1000000)) / 1000000)
            tmp["CR_avg"] = str(float(random.randint(0, 1000000)) / 1000000)
            tmp["LC_Rate_Factor"] = str(float(random.randint(0, 200)) / 100)
            dict_tmp["data"].append(tmp)
            del tmp
        list_tmp.append(dict_tmp)
        del dict_tmp
        dict_tmp = {"name": "转油二线", "data": []}
        for i in range(20):
            tmp = {}
            # tmp["Number"] = str(i)
            tmp["Time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(1665058704 + 2 * i))
            tmp["CR_max"] = str(float(random.randint(0, 1000000)) / 1000000)
            tmp["CR_avg"] = str(float(random.randint(0, 1000000)) / 1000000)
            dict_tmp["data"].append(tmp)
            del tmp
        list_tmp.append(dict_tmp)
        result["data"]=list_tmp
        return jsonify(result)

#http://127.0.0.1:5000/namedata
@app.route('/namedata', methods=['GET', 'POST'])
def namedata():
    if request.method == "GET":
        dict_tmp = {"COM1":  { "ID1": "转油二线上弯头", "ID2": "转油二线上弯头","ID3": "常减压基础桩"}
	    ,"COM2":{ "ID1": "转油三线上弯头", "ID2": "转油三线上弯头","ID3": "常减压基础桩"}}
        return jsonify(dict_tmp)
    elif request.method == "POST":
        str_tmp=request.stream.read() #get all data
        try:
            dict_tmp=json.loads(str_tmp)# note unicode
        except:
            return jsonify({"result": "data error"})
        print(dict_tmp["COM1"]["ID2"])
        #print(type(request.stream.read()))#post的内容
        return jsonify({"result": "ok"})
#####################old##################################################################
#http://127.0.0.1:5000/connectstatus
#@app.route('/connectstatus')
#def connectstatus():
#    dict_tmp={"com1":"red","com2":"green","com3":"green","com4":"grey","com5":"red"
#              ,"com6":"grey","com7":"red","com8":"green","com9":"red","com10":"red"
#              ,"com11":"red","com12":"green","com13":"red","com14":"green","com15":"red"
#              ,"com16":"green","com17":"green","com18":"green","com19":"red","com20":"grey"
#              ,"com21":"grey","com22":"grey","com23":"red","com24":"grey","com25":"red"
#              ,"com26":"green","com27":"red","com28":"green","com29":"red","com30":"grey"
#              ,"com31":"green","com32":"grey"}
#    return jsonify(dict_tmp)

#http://127.0.0.1:5000/data
#@app.route('/data')
#def datareturn():
#    dict_tmp = {"com": "1", "data": []}
#    for i in range(3000):
#        tmp = {}
#        tmp["Number"] = str(i)
#        tmp["Time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(1665058704 + 2 * i))
#        tmp["CR_max"] = str(float(random.randint(0, 1000000)) / 1000000)
#        tmp["CR_avg"] = str(float(random.randint(0, 1000000)) / 1000000)
#        tmp["LC_Rate_Factor"] = str(float(random.randint(0, 200)) / 100)
#        dict_tmp["data"].append(tmp)
#        del tmp
#    return jsonify(dict_tmp)

@app.route('/speeddata/<int:ComID>')
def speeddata(ComID):
    dict_tmp = {"data": []}
    for i in range(3000):
        tmp = {}
        tmp["Number"] = str(i)
        tmp["Time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(1665058704 + 2 * i))
        tmp["CR_max"] = str(float(random.randint(0, 1000000)) / 1000000)
        tmp["CR_avg"] = str(float(random.randint(0, 1000000)) / 1000000)
        tmp["LC_Rate_Factor"] = str(float(random.randint(0, 200)) / 100)
        dict_tmp["data"].append(tmp)
        del tmp
    dict_tmp["com"]=str(ComID)
    return jsonify(dict_tmp)
if __name__ == '__main__':
   app.run(debug = True)